package org.study.redis.api.util;

import java.util.List;
import java.util.Map;

/**
 * 类的描述
 *
 * @author doveylovey
 * @email 1135782208@qq.com
 * @date 2022年04月23日
 */
public class ConvertUtils {
    private ConvertUtils() {
        throw new AssertionError("不能实例化 ConvertUtils 工具类");
    }

    /**
     * 将 Map 的 key、value 转换为字符串数组
     *
     * @param map
     * @return java.lang.String[]
     */
    public static String[] map2Array(Map<String, String> map) {
        String[] array = new String[2];
        map.forEach((key, value) -> {
            array[0] = key;
            array[1] = value;
        });
        return array;
    }

    /**
     * 将 List 中每个 Map 的 key、value 转换为字符串数组
     *
     * @param listMap
     * @return java.lang.String[]
     */
    public static String[] listMap2Array(List<Map<String, String>> listMap) {
        String[] array = new String[2 * listMap.size()];
        for (int i = 0; i < listMap.size(); i++) {
            final int tmp = i;
            Map<String, String> map = listMap.get(i);
            map.forEach((key, value) -> {
                array[2 * tmp] = key;
                array[2 * tmp + 1] = value;
            });
        }
        return array;
    }

    /*public static void main(String[] args) {
        String[] result1 = map2Array(new HashMap<String, String>(1) {{
            put("name", "张三丰");
        }});
        System.out.println(JSON.toJSONString(result1));

        List<Map<String, String>> mapList = new ArrayList<Map<String, String>>() {{
            add(new HashMap<String, String>(1) {{
                put("name", "张三丰");
            }});
            add(new HashMap<String, String>(1) {{
                put("birthday", "1994-10-20");
            }});
            add(new HashMap<String, String>(1) {{
                put("salary", BigDecimal.valueOf(20000L).toPlainString());
            }});
            add(new HashMap<String, String>(1) {{
                put("job", "Java开发工程师");
            }});
            add(new HashMap<String, String>(1) {{
                put("address", "四川省成都市");
            }});
        }};
        String[] result2 = listMap2Array(mapList);
        System.out.println(JSON.toJSONString(result2));
    }*/
}
